home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / FILEIO.ASM < prev    next >
Assembly Source File  |  1993-01-04  |  7KB  |  260 lines

  1.         PAGE    60,132
  2.         TITLE   Routines to do low level file i/o
  3.  
  4. ; 005   14-Jan-87 fileio.asm
  5.  
  6. ;       Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  7.  
  8. _TEXT   SEGMENT  BYTE PUBLIC 'CODE'
  9. _TEXT   ENDS
  10. _DATA   SEGMENT  WORD PUBLIC 'DATA'
  11. _DATA   ENDS
  12. CONST   SEGMENT  WORD PUBLIC 'CONST'
  13. CONST   ENDS
  14. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  15. _BSS    ENDS
  16. DGROUP  GROUP   CONST,  _BSS,   _DATA
  17.         ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
  18.  
  19. _DATA   SEGMENT
  20.         EXTRN  _errno:WORD
  21.         EXTRN  __doserrno:WORD
  22. _DATA   ENDS
  23.  
  24. _TEXT   SEGMENT
  25.  
  26. ;*****************************************************************************
  27. ;
  28. ;  l_seek(fh,offset)
  29. ;  int fh;             file handle
  30. ;  long offset;        offset in file to seek to (from start of file)
  31. ;
  32. ;  Seek to a specific position in file.
  33. ;
  34. ;*****************************************************************************
  35.  
  36.         PUBLIC  _l_seek
  37.  
  38. _l_seek PROC    NEAR
  39.         push    bp
  40.         mov     bp,sp
  41.  
  42. ;       Basically lseek(fh,offset,SEEK_SET)
  43.  
  44.         mov     ax,4200h               ;seek from start of file
  45.         mov     bx,[bp+4]              ;file handle
  46.         mov     cx,[bp+8]              ;offset (most significant)
  47.         mov     dx,[bp+6]              ;       (least significant)
  48.         int     21h
  49.  
  50.         jnc     seekex                 ;jmp if no error
  51.  
  52.         call    fakerror               ;fake errno/_doserrno values
  53.         mov     ax,-1                  ;error, tell couldnt seek by
  54.         mov     dx,ax                  ;       returning -1L
  55.  
  56. seekex: mov     sp,bp
  57.         pop     bp
  58.         ret
  59.  
  60. _l_seek ENDP
  61.  
  62.  
  63. ;*****************************************************************************
  64. ;
  65. ;  readbuf(fh,bp,bl)
  66. ;  int fh;                     /* file handle */
  67. ;  char far *bp;               /* memory loc to read to */
  68. ;  unsigned int bl;            /* amount to read */
  69. ;
  70. ;  Read a buffer from the current location in file.
  71. ;
  72. ;*****************************************************************************
  73.  
  74.         PUBLIC  _readbuf
  75.  
  76. _readbuf PROC NEAR
  77.         push    bp
  78.         mov     bp,sp
  79.  
  80.         push    ds                     ;save current data seg
  81.  
  82.         mov     ah,3fh                 ;read file
  83.         mov     bx,[bp+4]              ;file handle
  84.         mov     cx,[bp+10]             ;bl (buffer length)
  85.         lds     dx,DWORD PTR [bp+6]    ;bp (buffer pointer)
  86.         int     21h
  87.  
  88.         pop     ds                     ;restore ds
  89.  
  90.         jnc     readex                 ;jmp if no error
  91.  
  92.         call    fakerror               ;fake errno/_doserrno values
  93.         xor     ax,ax                  ;error, tell caller 0 bytes read
  94.  
  95. readex: mov     sp,bp
  96.         pop     bp
  97.         ret
  98.  
  99. _readbuf ENDP
  100.  
  101.  
  102. ;*****************************************************************************
  103. ;
  104. ;  writebuf(fh,bp,bl)
  105. ;  int fh;                     /* file handle */
  106. ;  char far *bp;               /* memory loc to read to */
  107. ;  unsigned int bl;            /* amount to read */
  108. ;
  109. ;  Write a buffer to the current location in file.
  110. ;
  111. ;*****************************************************************************
  112.  
  113.         PUBLIC  _writebuf
  114.  
  115. _writebuf PROC NEAR
  116.         push    bp
  117.         mov     bp,sp
  118.  
  119.         push    ds                     ;save current data seg
  120.  
  121.         mov     ah,40h                 ;write file
  122.         mov     bx,[bp+4]              ;file handle
  123.         mov     cx,[bp+10]             ;bl (buffer length)
  124.         lds     dx,DWORD PTR [bp+6]    ;bp (buffer pointer)
  125.         int     21h
  126.  
  127.         pop     ds                     ;restore ds
  128.  
  129.         jnc     writex                 ;jmp if no error
  130.  
  131.         call    fakerror               ;fake errno/_doserrno values
  132.         mov     ax,-1                  ;tell caller error happened
  133.  
  134. writex: mov     sp,bp
  135.         pop     bp
  136.         ret
  137.  
  138. _writebuf ENDP
  139.  
  140.  
  141. ;*****************************************************************************
  142. ;
  143. ;  setftime(fh,date,time)
  144. ;  int fh;                     /* file handle */
  145. ;  unsigned int date;          /* date to set on file */
  146. ;  unsigned int time;          /* time to set on file */
  147. ;
  148. ;  Set the creation/revision date/time on a file.
  149. ;
  150. ;*****************************************************************************
  151.  
  152.         PUBLIC  _setftime
  153.  
  154. _setftime PROC NEAR
  155.         push    bp
  156.         mov     bp,sp
  157.  
  158.         mov     ax,5701h               ;set file date/time function
  159.         mov     bx,[bp+4]              ;file handle
  160.         mov     cx,[bp+8]              ;file time
  161.         mov     dx,[bp+6]              ;file date
  162.         int     21h
  163.  
  164.         mov     sp,bp                  ;no error checking done
  165.         pop     bp
  166.         ret
  167.  
  168. _setftime ENDP
  169.  
  170.  
  171. ;*****************************************************************************
  172. ;
  173. ;  getcdir(drive,buffer)
  174. ;  int drive;
  175. ;  char *buffer;
  176. ;
  177. ;  Get the current dir for given drive.
  178. ;
  179. ;*****************************************************************************
  180.  
  181.         PUBLIC  _getcdir
  182.  
  183. _getcdir PROC   NEAR
  184.         push    bp
  185.         mov     bp,sp
  186.  
  187.         push    si
  188.  
  189.         mov     ah,47h                 ;get current dir
  190.         mov     dl,BYTE PTR [bp+4]     ;drive code to DL
  191.         mov     si,[bp+6]              ;buffer ptr to DS:SI
  192.         mov     BYTE PTR [si],0        ;make sure its terminated if error
  193.         int     21h
  194.  
  195.         pop     si
  196.  
  197.         mov     sp,bp                  ;no error checking done
  198.         pop     bp
  199.         ret
  200.  
  201. _getcdir ENDP
  202.  
  203.  
  204. ;*****************************************************************************
  205. ;
  206. ;  setattrib(fn,attrs)
  207. ;  char *fn;                   /* file name */
  208. ;  unsigned int attrs;         /* attributes to set */
  209. ;
  210. ;  Set the file attributes
  211. ;
  212. ;*****************************************************************************
  213.  
  214.         PUBLIC  _setattrib
  215.  
  216. _setattrib PROC NEAR
  217.         push    bp
  218.         mov     bp,sp
  219.  
  220.         mov     ax,4301h               ;CHMOD dos function
  221.         mov     cx,[bp+6]              ;file attributes
  222.         mov     dx,[bp+4]              ;file name
  223.         int     21h
  224.  
  225.         mov     sp,bp                  ;no error checking done
  226.         pop     bp
  227.         ret
  228.  
  229. _setattrib ENDP
  230.  
  231.  
  232. ;*****************************************************************************
  233. ;
  234. ;   fakerror - fake errno and _doserrno values based on error code in ax
  235. ;
  236. ;   This is very very MSDOS/MSC dependent!
  237. ;
  238. ;*****************************************************************************
  239.  
  240. EACCES = 13    ; errno value for access error    *** these values correspond **
  241. EBADF  = 9     ; errno value for bad file handle *** to values in errno.h    **
  242.  
  243. fakerror PROC NEAR
  244.  
  245.         mov   __doserrno,ax            ;save raw error code in _doserrno
  246.  
  247.         cmp   ax,6                     ;did dos say bad file handle?
  248.         jne   access
  249.  
  250.         mov   _errno,EBADF             ;bad file handle
  251.         ret
  252.  
  253. access: mov   _errno,EACCES            ;return any other as access error
  254.         ret
  255.  
  256. fakerror ENDP
  257.  
  258. _TEXT   ENDS
  259. END
  260.